package com.chenfan.mcn.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.mcn.enums.ApprovalStatusEnum;
import com.chenfan.common.extension.mybatisplus.enhanced.EnhancedBaseMapper;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.YearFrameContract;
import com.chenfan.mcn.model.dto.YearFrameContractSearchDTO;
import com.chenfan.mcn.model.vo.CusYearContractVO;
import com.chenfan.mcn.model.vo.YearFrameContractVO;
import com.chenfan.mcn.model.vo.YearFrameVO;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 年框合同 Mapper 接口
 * </p>
 *
 * @author lizhejin
 * @since 2021-01-19
 */
public interface YearFrameContractMapper extends EnhancedBaseMapper<YearFrameContract> {

    /**
     * 年框合同分页数据
     *
     * @param page                 {@link McnPage}
     * @param yearFrameContractSearchDTO {@link YearFrameContractSearchDTO}
     * @return
     */
    Page<YearFrameVO> getList(McnPage<YearFrameVO> page, @Param("dto") YearFrameContractSearchDTO yearFrameContractSearchDTO);

    /**
     * 根据折扣审批单id获取年框合同
     *
     * @param discountId {@link Long}
     * @return {@link}
     */
    default YearFrameContract geteffectiveContract(Long discountId) {
        QueryWrapper<YearFrameContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(YearFrameContract::getDiscountId, discountId)
                .eq(YearFrameContract::getIsDelete, Boolean.FALSE)
                .eq(YearFrameContract::getYearStatus, ApprovalStatusEnum.PASSED.code())
                .le(YearFrameContract::getCooperationStartDate, new Date())
                .ge(YearFrameContract::getCooperationEndDate, new Date());
        return this.selectOne(queryWrapper);
    }

    /**
     * 导出excel
     *
     * @param yearFrameContractSearchDTO {@link YearFrameContractSearchDTO}
     * @return {@link List}
     */
    List<YearFrameVO> exportList(@Param("dto") YearFrameContractSearchDTO yearFrameContractSearchDTO);

    /**
     * 获取所有的年框合同
     *
     * @param yearFrameContractSearchDTO {@link YearFrameContractSearchDTO}
     * @return {@link List<String>}
     */
    List<YearFrameContractVO> getYearCode(@Param("dto") YearFrameContractSearchDTO yearFrameContractSearchDTO);

    /**
     * 根据年框合同编号获取合同
     *
     * @param yearCode {@link String}
     * @return {@link YearFrameContract}
     */
    YearFrameContract selectByYearCode(String yearCode);

    /**
     * 校验同一客户，同一折扣审批单，非作废状态下是否已经存在年框合同
     *
     * @param targetType   {@link Integer}
     * @param customerId   {@link Long}
     * @param discountCode {@link String}
     * @return {@link YearFrameContract}
     */
    YearFrameContract isExist(Integer targetType, Long customerId, String discountCode);

    /**
     * 根据id获取年框合同
     *
     * @param yearId    {@link Long}
     * @param processId {@link Long}
     * @return {@link YearFrameContractVO}
     */
    YearFrameContractVO selectByYearId(Long yearId, Long processId);

    /**
     * 根据折扣审批单编号获取年框合同
     *
     * @param discountCode {@link String}
     * @return {@link YearFrameContract}
     */
    YearFrameContract selectByDiscountCode(String discountCode);

    /**
     * 客户关联的年框合同
     *
     * @param customerId {@link Long}
     * @param mcnPage    {@link McnPage<CusYearContractVO> mcnPage}
     * @param processId  {@link Long}
     * @return
     */
    Page<CusYearContractVO> getCusYearContract(McnPage<CusYearContractVO> mcnPage, @Param("customerId") Long customerId, @Param("processId") Long processId);

    /**
     * 校验合作时间
     *
     * @param yearFrameContractVO {@link YearFrameContractVO}
     * @return {@link YearFrameContract}
     */
    List<YearFrameContract> verifyCooperationTime(@Param("yearFrameContractVO") YearFrameContractVO yearFrameContractVO);

    /**
     * 获取执行单计算分成关联的年框合同
     * @param targetType
     * @param customerId
     * @return
     */
    List<YearFrameContract> getYearFrameContract(@Param("targetType") Integer targetType, @Param("customerId") Long customerId);
}
